// 初始化连接数据库
const Seq = require('sequelize/index')
const config = require('../../config')
const { lowdb } = require('./../lowdb/index')
const configMysql = lowdb.read().get('mysql').value()
let sequelize;
if (configMysql.database) {
  sequelize = new Seq(
    configMysql.database,
    configMysql.username,
    configMysql.password,
    {
      dialect: 'mysql',
      host: configMysql.host,
      port: configMysql.port,
      define: {
        underscored: true
      }
    }
  )
} else {
  sequelize = new Seq(
    config.sqldb.database,
    config.sqldb.username,
    config.sqldb.password,
    {
      dialect: 'mysql',
      dialectOptions: {
        charset: 'utf8mb4',
        collate: 'utf8mb4_general_ci',
        supportBigNumbers: true,
        bigNumberStrings: true
      },
      host: config.sqldb.host,
      port: config.sqldb.port,
      timezone: config.sqldb.timezone,
      define: {
        // 字段以下划线（_）来分割（默认是驼峰命名风格）
        underscored: true
      }
    }
  )
}
module.exports = {
  sequelize
}